﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBuy.DataAccessLayer;
using System.Collections;
using System.Data;

namespace IBuy.Entity
{
    class GoodType
    {
        #region 私有成员
        private String typeID;
        private String typeName;
        private IDatabase database;
        #endregion

        #region 公有成员
        //类型编号
        public String TypeID
        {
            get { return typeID; }
            set { typeID = value; }
        }
        //类型名称
        public String TypeName
        {
            get { return typeName; }
            set { typeName = value; }
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public GoodType()
        {
            DatabaseFactory databaseFactory = new OledbDatabaseFactory();
            database = databaseFactory.GetDatabase();
        }

        /// <summary>
        /// 添加一种物品类型
        /// </summary>
        /// <param name="cols">Hashtable类型，物品的属性集合</param>
        /// <returns>bool类型，是否成功</returns>
        public bool InsertGoodsType(Hashtable cols)
        {
            return database.Insert("TYPE", cols);
        }

        /// <summary>
        /// 更新一种物品类型
        /// </summary>
        /// <param name="cols">Hashtable类型，待更改的物品的属性集合</param>
        /// <param name="where">String类型，判断条件</param>
        /// <returns>bool类型，是否成功</returns>
        public bool UpdateGoodsType(Hashtable cols, String where)
        {
            return database.Update("TYPE", cols, where);
        }

        /// <summary>
        /// 删除一种物品类型
        /// </summary>
        /// <param name="typeName">String类型，物品名称</param>
        /// <param name="where">String类型，判断条件</param>
        /// <returns></returns>
        public bool DeleteGoodsType(String typeName)
        {
            return database.Delete("TYPE", " where TYPENAME='"+typeName+"'");
        }
        /// <summary>
        /// 获得所有的商品种类
        /// </summary>
        /// <returns>DataSet类型，所有商品种类的集合</returns>
        public DataSet GetAllType()
        {
            return database.GetDataSet("select TYPEID,TYPENAME from TYPE");
        }
        /// <summary>
        /// 根据种类名获得种类ID
        /// </summary>
        /// <param name="TypeName">string 类型，种类名</param>
        /// <returns>String 类型，种类ID</returns>
        public String GetTypeIDByName(string TypeName)
        {
            DataRow row= database.GetDataRow("select TYPEID from TYPE where TYPENAME='" + TypeName + "'");
            if (row != null)
            {
                return row[0].ToString();
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
